/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.testis.utils;

import java.util.LinkedList;

/**
 *
 * @author emir
 */
public class SortAlphabetically {
    public static String[] sortAlphabetically(String[] source, boolean ascending) {
        LinkedList<String> result = new LinkedList<String>();
        int compareResult;
        boolean placed;
        for (int i = 0; i < source.length; i++) {
            placed=false;
            for (int j = 0; !placed && j < result.size(); j++) {
                compareResult = result.get(j).compareTo(source[i]);
                if (ascending) {
                    if (compareResult<0) {
                        if (j<result.size()) {
                            result.add(j,source[i]);
                            placed=true;
                        }
                    }
                } else {
                    if (compareResult>0) {
                        if (j<result.size()) {
                            result.add(j,source[i]);
                            placed=true;
                        }
                    }
                }
            }
            if (!placed) {
                result.add(source[i]);
            }
        }
        return result.toArray(new String[result.size()]);
    }
}
